﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import flash.events.*;
    import Engine.Helpers.*;
    import flash.utils.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Mechanics.GameEventMechanics.*;

    public class BusinessVisitBatchManager implements IGameWorldStateObserver {

        public static const MAX_BATCH_DELAY_SECONDS:Number = 20;
        public static const MAX_NUMBER_OF_UPDATES:int = 100;

        protected var m_visits:BusinessVisitStorage;
        protected var m_updates:int = 0;
        protected var m_timer:Timer = null;

        public function BusinessVisitBatchManager(_arg1:GameWorld){
            this.m_visits = new BusinessVisitStorage();
            super();
            _arg1.addObserver(this);
        }
        public function initialize():void{
        }
        public function cleanUp():void{
            this.flushVisits();
        }
        public function onGameLoaded(_arg1:Object):void{
        }
        public function onResourceChange(_arg1:MapResource, _arg2:Vector3, _arg3:Vector3):void{
        }
        public function addVisit(_arg1:Business, _arg2:int, _arg3:int):void{
            var _local4:Number = _arg1.getId();
            this.m_visits.add(_local4, _arg2, _arg3);
            this.m_updates++;
            if (this.m_timer == null){
                this.m_timer = new Timer((MAX_BATCH_DELAY_SECONDS * 1000));
                this.m_timer.addEventListener(TimerEvent.TIMER, this.onTimerComplete);
                this.m_timer.start();
            };
            if (this.m_updates >= MAX_NUMBER_OF_UPDATES){
                this.flushVisits();
            };
        }
        public function onBusinessAction():void{
            this.flushVisits();
        }
        protected function onTimerComplete(_arg1:TimerEvent):void{
            this.flushVisits();
        }
        protected function flushVisits():void{
            if (this.m_timer != null){
                this.m_timer.removeEventListener(TimerEvent.TIMER, this.onTimerComplete);
                this.m_timer.stop();
                this.m_timer = null;
            };
            if (this.m_updates > 0){
                GameTransactionManager.addTransaction(new TBusinessVisitBatch(this.m_visits));
            };
            this.m_visits = new BusinessVisitStorage();
            this.m_updates = 0;
            MerchantCustomerMechanic.forceFlush();
        }

    }
}//package Classes.sim 
